﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    [Module("FrmAccountinfoList")]
    public partial class FrmAccountinfoList : ModuleForm
    {
        readonly AccountinfoBLL _accountinfobll;
        public FrmAccountinfoList()
        {
            InitializeComponent();

            _accountinfobll = new AccountinfoBLL();
            cboIsbank.SelectedIndex = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryInfo()
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _accountinfobll.Query(cboIsbank.SelectedItem.ToString(), txtKey.Text.Trim());
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            var f = new FrmAccountinfoEdit();
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;
            var id = ((Accountinfo)dgvList.CurrentRow.DataBoundItem).Id;

            var obj = _accountinfobll.Get(id);
            var f = new FrmAccountinfoEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;
            var obj = dgvList.CurrentRow.DataBoundItem as Accountinfo;
            var text = string.Format(@"您要确定要“删除”当前收付款账户“{0}”吗？", obj.Accountname);
            if (MessageBox.Show(text, "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _accountinfobll.Delete(obj.Id);
                QueryInfo();
            }
        }

        private void tsbPrint_Click(object sender, EventArgs e)
        {

        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            string msgLog;
            ExcelUtil.ExportToExcel(dgvList, "收付款账户信息", out msgLog);
            if (msgLog != null)
                MessageBox.Show(msgLog, @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryInfo();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            tsbEdit_Click(null, null);
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ControlUtil.DataGridRowPostPaint(sender, e);
        }

        private void cboIsbank_SelectedIndexChanged(object sender, EventArgs e)
        {
            QueryInfo();
        }
    }
}
